<?
/**
 * VoiceOne Settings/Rules/outgoing/edit
 *
 * Frontend to provide access to the rules system in voiceone.
 * @package voiceone
 * @subpackage  settings_rules
 */
  include ("../../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
 	sajax_init();
    
  //================ PAGE LOGIC ================
  if (count($_POST)>0) {
    if (is_array($_POST["listRight"])) $resource=implode("|",$_POST["listRight"]);
    $entry=Array(
      "id"  => $_POST["id"],
      "rule" => $_POST["rule"],
      "priority" => $_POST["priority"],
      "timesegment" => $_POST["timesegment"], 
      "action" => $_POST["action"], 
      "resource" => $resource 
    );

    if (substr($_POST["group"],0,2)=="g_")  $entry["group"]=substr($_POST["group"],2); 
    else $entry["entity"]=$_POST["group"]; 
    if ($_POST["kind"]==GROUP_OF_ALL) $entry["group"]=GROUP_OF_ALL;

    //save the group informations
    $ret=$asterisk->putOutgoingRule($entry);
    if ($ret>0) {
      $alert="<div class=\"AlertGood\">".$cwlang["ruleout"]["createRuleYes"]."</div>";
      $_GET["id"]=$ret;
    } else $alert="<div class=\"AlertBad\">".$cwlang["ruleout"]["createRuleNo"]."</div>";
    
    goToMainPage($alert);
  }
  //--------------------------------------------------------------------------
  
  //if modify, read the group informations
  if (isset($_GET["id"])) {
    $ret=$asterisk->getOutgoingRule($_GET["id"]);
    if ($err=$asterisk->getError()) die("Error: $err");
    $id=$ret[0]["id"];
    $rule=$ret[0]["rule"];
    $priority=$ret[0]["priority"];
    $timesegment=$ret[0]["timesegment"];  
    $group=$ret[0]["group"]; 
    $entity=$ret[0]["entity"];  
    $resource=split("\|",$ret[0]["resource"]);  
    $provider=$ret[0]["provider"];  
    $action=$ret[0]["action"]; 
  }  

  //get the time segment available
  $tsHtml.="<option value=\"0\">[any time]</option>\n";
  $ret=$asterisk->getTimeSegment(0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach ($ret as $ts) {
    $id=$ts["id"];
    $name=$ts["name"];
    if ($id==$timesegment) $selected=" selected=\"selected\" ";
    else $selected="";
    $tsHtml.="<option value=\"$id\" $selected>$name</option>\n";
  }
    
  //get the trunk available
  $ret=$asterisk->getTrunk(0,0);
  if ($err=$asterisk->getError()) die("Error: $err");
  if (is_array($ret)) 
  foreach ($ret as $tr) {
    $id=$tr["id"];
    $name=$tr["name"];
    if (!@in_array($id,$resource)) $listLeft.="<option value=\"$id\" >$name</option>";    
  }

  //get the trunk already selected
  if (is_array($resource)) 
  foreach ($resource as $r) {
    $id=$r;
    $tr=$asterisk->getTrunk($id,0);
    $name=$tr[0]["name"];
    if ($id=="0") $name="[any trunk]";
    $listRight.="<option value=\"$id\" >$name</option>";
  }
  
  //adjust the trunk list
  if (isset($_GET["id"])) {
    if (!in_array("0",$resource)) $listLeft.="<option value=\"0\">[any trunk]</option>\n";
  } else $listRight.="<option value=\"0\">[any trunk]</option>\n";


  
  $id=$_REQUEST["id"];
  $rule=$_REQUEST["rule"];  
  //================ MAIN CONTENTS ================
  ob_start();
  ?>
  <?$sel="outgoing";include("../../navigation.php");?> 
  
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=$cwlang["ruleout"]["editruleTitle"]?></h1>
    <h3><a href="../" class="admin" ><?=$cwlang["ruleout"]["return"]?></a></h3>
  </div>
  
  <div id="basic_form">
    <form id="outgoingrule" action="<?=$PHP_SELF?><?if (isset($_GET["id"])) echo "?id=".$_GET["id"];?>" method="post" onSubmit=" return selectAll(getElementById('listRight'));">
      
    	<h2><?=$cwlang["ruleout"]["timesegment"]?></h2>
      <p class="blockintro"><?=$cwlang["ruleout"]["timesegmentDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required" width="160"><label for="timesegment"><?=$cwlang["ruleout"]["timesegment"]?>:</label></td>
            <td colspan="2"><select id="timesegment" name="timesegment" ><?=$tsHtml?></select></td>
          </tr>
        </table>
      </div>

<?    echo listGroupEntity($js, $group, $entity);?>      
      
    	<h2><?=$cwlang["ruleout"]["action"]?></h2>
      <p class="blockintro"><?=$cwlang["ruleout"]["actionDesc"]?></p>
      <div class="block">
        <table>
          <tr>
            <td class="required" width="160"><label for="action"><?=$cwlang["ruleout"]["action"]?>:</label></td>
            <td >
              <select id="action" name="action">
                <option value="<?=RULE_ALLOW?>" <?if ($action==RULE_ALLOW) echo "selected='selected'";?>><?=$cwlang["ruleout"]["allow"]?></option>
                <option value="<?=RULE_DENY?>" <?if ($action==RULE_DENY) echo "selected='selected'";?>><?=$cwlang["ruleout"]["deny"]?></option>
              </select>
            </td>
          </tr>
        </table>
      </div>

      <div id="trunkDiv">
      	<h2><?=$cwlang["ruleout"]["trunk"]?></h2>
        <p class="blockintro"><?=$cwlang["ruleout"]["trunkDesc"]?></p>
        <div class="block">
          <table>
            <tr>
              <td class="required">
                <div ><?=$cwlang["ruleout"]["resources"]?>:</div>
                <select multiple size="5"  name="listLeft[]" id="listLeft"><?=$listLeft?></select>
              </td>
              <td>   
                <div><input type="button" onclick="moveDualList( document.getElementById('listLeft'),  document.getElementById('listRight'), false )" name="Add     >>"  value="<?=$cwlang["ruleout"]["add"]?>" style="width:100px"></div>
                <div><input type="button" onclick="moveDualList( document.getElementById('listRight'), document.getElementById('listLeft'),  false )" name="Add     <<"  value="<?=$cwlang["ruleout"]["remove"]?>" style="width:100px"></div>
                <div><input type="button" onclick="moveDualList( document.getElementById('listLeft'),  document.getElementById('listRight'), true  )" name="Add All >>"  value="<?=$cwlang["ruleout"]["addAll"]?>" style="width:100px"></div>
                <div><input type="button" onclick="moveDualList( document.getElementById('listRight'), document.getElementById('listLeft'),  true  )" name="Add All <<"  value="<?=$cwlang["ruleout"]["removeAll"]?>" style="width:100px"></div>
              </td>
              <td class="required"><div ><?=$cwlang["isdngrp"]["groupPorts"]?>:</div>
                <div style="float: left">
                  <input type="image"  src="<?=$relBasePath?>public/img/icon_up.gif"  onclick="moveOptions ( document.getElementById('listRight'), -1); return false" name="Up"  value="Down" style="display: block">
                  <input type="image"  src="<?=$relBasePath?>public/img/icon_down.gif"  onclick="moveOptions ( document.getElementById('listRight'), 1); return false;" name="Down"  value="Down" style="display: block">
                </div>
                  <select multiple size="5" name="listRight[]" id="listRight"><?=$listRight?></select>
              </td>
            </tr>
          </table>
        </div>
      </div>      
      
      <?=$numHidden?>      
      <div class="action">
        <input type="hidden" name="id" value="<?=$id?>" />
        <input type="hidden" name="rule" value="<?=$rule?>" />
        <input type="hidden" name="priority" value="<?=$priority?>" />
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-cancel.gif" alt="Cancel" onclick="location.href='../'; return false;"/>
        <input type="image" src="<?=$relBasePath?>/public/img/<?=$cwLanguage?>/b-save.gif" alt="Save" onclick="return checkDestination();" />
      </div>
    </form>
  </div>  
  
  <script>
    function checkDestination(){
      var selectList = document.forms[0].elements.kind;
      for (var i=0; i<selectList.length; i++) { 
        if ((selectList[i].value==1)&&(selectList[i].selected==true)) return true;      
      }
      for (var i=0; i<document.forms[0].elements.length; i++) { 
        if(document.forms[0].elements[i].name=="group"){
          if(document.forms[0].elements[i].checked) return true;
        } 
      } 
      alert('<?=$cwlang["ruleout"]["script1"]?>');
      return false;
    }
  </script>
  
  
  <?

  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
        <div class="Sidebar">
          <h1><?=$cwlang["ruleout"]["editruleSB"]?></h1>
          <p><?=$cwlang["ruleout"]["editruleSBdesc"]?>
        </div>
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================
 	sajax_handle_client_request();

  $smarty->assign("section", "rules");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->assign("ajax", $js);
  $smarty->display('settings.tpl');
?>

